#pragma once
#include <odb/core.hxx>
#include <odb/nullable.hxx>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <string>
#include "chat_session_member.hxx"
enum ChatSessionType {
    SINGLE = 0, // 单聊
    GROUP = 1   // 群聊
};

#pragma db object
class ChatSession {
public:
    ChatSession() = default;
    ChatSession(const std::string &chat_session_id, const std::string &chat_session_name, ChatSessionType chat_session_type)
        : _chat_session_id(chat_session_id)
        , _chat_session_name(chat_session_name)
        , _chat_session_type(chat_session_type)
    {}

    unsigned long id() const { return _id; }
    std::string chat_session_id() const { return _chat_session_id; }
    std::string chat_session_name() const { return _chat_session_name; }
    ChatSessionType chat_session_type() const { return _chat_session_type; }

    void set_chat_session_id(const std::string &chat_session_id) { _chat_session_id = chat_session_id; }
    void set_chat_session_name(const std::string &chat_session_name) { _chat_session_name = chat_session_name; }
    void set_chat_session_type(ChatSessionType chat_session_type) { _chat_session_type = chat_session_type; }
private:
    friend class odb::access;

    #pragma db id auto
    unsigned long _id; // 数据库分配的 ID

    #pragma db type("varchar(64)") index unique
    std::string _chat_session_id; // 聊天会话 ID

    #pragma db type("varchar(64)")
    std::string _chat_session_name; // 聊天会话名称

    #pragma db type("tinyint")
    ChatSessionType _chat_session_type; // 聊天会话类型（单聊、群聊等）
};


#pragma db view object(ChatSession = css)\
                object(ChatSessionMember = csm1 : css::_chat_session_id == csm1::_chat_session_id\
                    && css::_chat_session_type == ChatSessionType(0))\
                object(ChatSessionMember = csm2 : css::_chat_session_id == csm2::_chat_session_id\
                    && csm1::_user_id != csm2::_user_id)\
                query((?))
struct SingleChatSession {
    #pragma db column(css::_chat_session_id)
    std::string _chat_session_id;

    #pragma db column(csm2::_user_id)
    std::string _friend_id;
};




#pragma db view object(ChatSession = css)\
                object(ChatSessionMember = csm : css::_chat_session_id == csm::_chat_session_id\
                    && css::_chat_session_type == ChatSessionType(1))\
                query((?))
struct GroupChatSession {
    #pragma db column(css::_chat_session_id)
    std::string _chat_session_id;

    #pragma db column(css::_chat_session_name)
    std::string _chat_session_name;
};